import os, string, sys, os.path, re
import xlwt, xlrd
from xlutils.copy import copy

WorkSpace=os.getcwd()

def check_input_args(args):
	efusehash_file=args[1]
	eFusedata_excel=args[2]
	return efusehash_file,eFusedata_excel

def Read_Hash(filename):
	if os.path.isfile(filename):
		fp=open(filename)
		for line in fp.readlines():
			one_line=line.split(" ")
		fp.close()
	return one_line

def Write_data(fb,sheet,rows_num,rows_start):
	for i in range(rows_num):
		efuse_data_1=sheet.cell(rows_start+i, 7).value
		efuse_data_2=sheet.cell(rows_start+i, 8).value
		efuse_data_3=sheet.cell(rows_start+i, 24).value

		efuse_data_1=hex(int(efuse_data_1.strip('[]')))
		efuse_data_1=efuse_data_1[2:].zfill(2)
		efuse_data_1=bytes(efuse_data_1, encoding = "utf8")

		efuse_data_2=str(int(efuse_data_2)).zfill(2)
		efuse_data_2=efuse_data_2.ljust(6,'0')
		efuse_data_2=bytes(efuse_data_2, encoding = "utf8")

		efuse_data_3=str(int(efuse_data_3)).zfill(2)
		efuse_data_3=efuse_data_3.ljust(8,'0')
		efuse_data_3=bytes(efuse_data_3, encoding = "utf8")
		fb.write(efuse_data_1)
		fb.write(efuse_data_2)
		fb.write(efuse_data_3)

def Write_hash_data(fb,sheet,rows_num,rows_start):
	for i in range(rows_num):
		efuse_data_1=sheet.cell(rows_start+i, 7).value
		efuse_data_2=sheet.cell(rows_start+i, 8).value
		efuse_data_3=sheet.cell(rows_start+i, 24).value

		efuse_data_1=hex(int(efuse_data_1.strip('[]')))
		efuse_data_1=efuse_data_1[2:].zfill(2)
		efuse_data_1=bytes(efuse_data_1, encoding = "utf8")

		efuse_data_2=str(int(efuse_data_2)).zfill(2)
		efuse_data_2=efuse_data_2.ljust(6,'0')
		efuse_data_2=bytes(efuse_data_2, encoding = "utf8")

		efuse_data_3=bytes(efuse_data_3, encoding = "utf8")
		fb.write(efuse_data_1)
		fb.write(efuse_data_2)
		fb.write(efuse_data_3)

def Write_data_fix_hex(fb,sheet,rows_num,rows_start):
	for i in range(rows_num):
		efuse_data_1=sheet.cell(rows_start+i, 7).value
		efuse_data_2=sheet.cell(rows_start+i, 8).value
		efuse_data_3=sheet.cell(rows_start+i, 24).value

		efuse_data_1=hex(int(efuse_data_1.strip('[]')))
		efuse_data_1=efuse_data_1[2:].zfill(2)
		efuse_data_1=bytes(efuse_data_1, encoding = "utf8")

		efuse_data_2=str(int(efuse_data_2)).zfill(2)
		efuse_data_2=efuse_data_2.ljust(6,'0')
		efuse_data_2=bytes(efuse_data_2, encoding = "utf8")

		efuse_data_3=str(efuse_data_3)[2:]
		efuse_data_3=bytes(efuse_data_3, encoding = "utf8")
		fb.write(efuse_data_1)
		fb.write(efuse_data_2)
		fb.write(efuse_data_3)

def main(argv):
	efusehash_file,eFusedata_excel=check_input_args(sys.argv)
	real_hash_file=os.path.join(WorkSpace,efusehash_file)
	real_eFusedata_excel=os.path.join(WorkSpace,eFusedata_excel)
	hash_list=Read_Hash(real_hash_file)

	#write hash to excel
	real_eFusedata_wbk = xlrd.open_workbook(real_eFusedata_excel)
	real_eFusedata_wbk_data = copy(real_eFusedata_wbk)
	efusedata_sheet=real_eFusedata_wbk_data.get_sheet(2)

	for i in range(8):
		efusedata_sheet.write(9+i,24,hash_list[i])
	real_eFusedata_wbk_data.save(real_eFusedata_excel)

	#write excel to file
	efuse_excel=xlrd.open_workbook(real_eFusedata_excel)
	efuse_data_sheet=efuse_excel.sheets()[2]
	TMP_file=os.path.join(os.path.join(WorkSpace,"eFusedata"))
	if os.path.isfile(TMP_file):
		os.remove(TMP_file)
	fb=open(TMP_file,"ab")

	Write_data(fb,efuse_data_sheet,1,8)
	Write_hash_data(fb,efuse_data_sheet,8,9)
	Write_data(fb,efuse_data_sheet,24,17)
	Write_data(fb,efuse_data_sheet,10,41)
	Write_data(fb,efuse_data_sheet,6,52)
	Write_data(fb,efuse_data_sheet,2,59)
	Write_data_fix_hex(fb,efuse_data_sheet,8,62)
	Write_data(fb,efuse_data_sheet,1,70)
	Write_data(fb,efuse_data_sheet,1,72)
	Write_data_fix_hex(fb,efuse_data_sheet,4,73)
	Write_data(fb,efuse_data_sheet,1,77)
	Write_data(fb,efuse_data_sheet,2,79)
	Write_data(fb,efuse_data_sheet,2,82)

	fb.close()

if __name__ == '__main__':
	main(sys.argv)


